package com.bawei.park2302.repair

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import androidx.activity.viewModels
import androidx.lifecycle.lifecycleScope
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bawei.lib_base.utils.utils.entity.ParkBeanItem
import com.bawei.lib_base.utils.utils.entity.RepairBeanItem
import com.bawei.lib_base.utils.utils.mvi.BaseActivity
import com.bawei.lib_base.utils.utils.mvi.UiState
import com.bawei.lib_base.utils.utils.request.RequestType
import com.bawei.park2302.R
import com.bawei.park2302.databinding.ActivityRepairDetailBinding
import com.blankj.utilcode.util.ToastUtils
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch

@Route(path = "/app/RepairDetailActivity")
@AndroidEntryPoint
class RepairDetailActivity : BaseActivity<ActivityRepairDetailBinding,RepairIntent,RepairViewModel>() {
    override val binding: ActivityRepairDetailBinding by lazy {
        ActivityRepairDetailBinding.inflate(layoutInflater)
    }
    override val vm: RepairViewModel by viewModels<RepairViewModel>()

    @Autowired
    @JvmField
    var repairId = 0

    override fun initView() {
        ARouter.getInstance().inject(this)

        vm.sendIntent(RepairIntent.repairDetail(repairId))

        binding.repDetailOk.setOnClickListener {
            binding.bean?.processingStatus="处理中"
            vm.sendIntent(RepairIntent.repairOrders(repairId,binding.bean!!))
        }

        binding.repDetailRefause.setOnClickListener {
            binding.bean?.processingStatus="已完成"
            vm.sendIntent(RepairIntent.repairOrders(repairId,binding.bean!!))
        }
    }

    override fun initData() {
        lifecycleScope.launch {
            vm.uiState.collect{
                when(it){
                    is UiState.onSuccess<*> ->{
                        when(it.type){
                            RequestType.SPACE ->{
                                ToastUtils.showLong("审批完成")
                                finish()
                            }

                            RequestType.DETAIL ->{
                                val item = it.data as RepairBeanItem
                                binding.bean = item
                                if(item.processingStatus!="已完成"){
                                    binding.repDetailGroup.visibility= View.VISIBLE
                                }
                                if(item.processingStatus=="处理中"){
                                    binding.repDetailProCircle2.setBackgroundResource(R.drawable.shape_solid_darkgreen)
                                }else if(item.processingStatus=="已完成"){
                                    binding.repDetailProCircle2.setBackgroundResource(R.drawable.shape_solid_darkgreen)
                                    binding.repDetailProCircle3.setBackgroundResource(R.drawable.shape_solid_darkgreen)
                                }
                            }
                            else -> {}
                        }
                    }
                    else -> {}
                }
            }
        }
    }
}